package com.stu.student_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.student_back_end.controller.dto.CountDTO;
import com.stu.student_back_end.controller.dto.PageDTO;
import com.stu.student_back_end.entity.TblBuilding;
import com.stu.student_back_end.entity.TblDorm;
import com.stu.student_back_end.mapper.TblBuildingMapper;
import com.stu.student_back_end.mapper.TblDormMapper;
import com.stu.student_back_end.service.ITblDormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 宿舍service实现类
 */
@Service
public class TblDormServiceImpl extends ServiceImpl<TblDormMapper, TblDorm> implements ITblDormService {

    @Autowired
    private TblBuildingMapper buildingMapper;

    @Override
    public Page<TblDorm> queryDorm(PageDTO pageDTO) {
        Page<TblDorm> page =new Page<>(pageDTO.getCurrentPage(),pageDTO.getSizePage());
        Page<TblDorm> buildPage = new Page<>();
        //判断是否带关键字
        if(!(pageDTO.getKeyWord() == "")) {
            //不为空
            buildPage = page(page, new LambdaQueryWrapper<TblDorm>().like(TblDorm::getDormName, pageDTO.getKeyWord()));
        }else {
            buildPage = page(page);
        }
        return buildPage;
    }

    @Override
    public CountDTO queryDormCount() {
        CountDTO countVo = new CountDTO();
        countVo.setCount(baseMapper.queryDormCount());
        countVo.setName("宿舍");
        return countVo;
    }

    /**
     * 查询一栋楼的人数
     * @return
     */
    @Override
    public CountDTO queryBuildNameCount() {
        List<CountDTO> list = new ArrayList<>();
        List<TblBuilding> tblBuildings = buildingMapper.selectList(null);
        CountDTO countDTO2 = new CountDTO();
        for (TblBuilding building : tblBuildings) {
            CountDTO countDTO = new CountDTO();
            int count = baseMapper.queryDormBuildingCount(building.getBuildName());
            countDTO.setBuildName(building.getBuildName());
            countDTO.setSum(count);
            list.add(countDTO);
        }
        countDTO2.setCounts(list);
        return countDTO2;
    }
}
